local qingzi = fk.CreateSkill {
  name = "qingzi",
}

qingzi:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start and
      table.find(player.room:getOtherPlayers(player, false), function(p) return #p:getCardIds("e") > 0 end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room:getOtherPlayers(player, false), function(p)
      return #p:getCardIds("e") > 0 end), Util.IdMapper)
    local tos = room:askForChoosePlayers(player, targets, 1, 999, "#qingzi-choose", self.name, true)
    if #tos > 0 then
      room:sortPlayersByAction(tos)
      event:setCostData(self,tos)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(event:getCostData(self)) do
      if player.dead then return end
      local p = room:getPlayerById(id)
      if not p.dead and #p:getCardIds("e") > 0 then
        local c = room:askForCardChosen(player, p, "e", self.name)
        room:throwCard(c, self.name, p, player)
        if not p:hasSkill("ol_ex__shensu", true) and not p.dead then
          room:addTableMark(player, "qingzi_target", p.id)
          room:handleAddLoseSkills(p, "ol_ex__shensu", nil, true, false)
        end
      end
    end
  end,


})

local func={
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("qingzi_target") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(player:getMark("qingzi_target")) do
      local p = room:getPlayerById(id)
      room:handleAddLoseSkills(p, "-ol_ex__shensu", nil, true, false)
    end
    room:setPlayerMark(player, "qingzi_target", 0)
  end,
}
qingzi:addEffect(fk.TurnStart,func )
qingzi:addEffect(fk.Death,func )

return qingzi